import Vue from 'vue'
import Vuex from 'vuex'
import WebSocket from '@/util/webScoket'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    sid: '',
    isLogin: false,
    token: '',
    userInfo: {},
    ws: null,
    num: 0
  },
  mutations: {
    initWebSocket: (state, value) => {
      state.ws = new WebSocket()
      state.ws.init()
    },
    setSid: (state, value) => {
      state.sid = value
    },
    // 设置token
    setToken: (state, value) => {
      state.token = value
      localStorage.setItem('token', value)
    },
    // 设置用户信息
    setUserInfo: (state, value) => {
      state.userInfo = value
      localStorage.setItem('userInfo', JSON.stringify(value))
    },
    // 设置login状态
    setLoginStatus: (state, value) => {
      state.isLogin = value
    },
    setMessage: (state, val) => {
      state.num = val.message ? val.message : 0
    },
    setNum: (state, val) => {
      state.num = val
    }
  },
  actions: {
    message ({ commit }, msg) {
      commit('setMessage', msg)
    }
  },
  modules: {}
})
